package org.openarchitectureware.recipe2.findbugs;

import java.util.ArrayList;
import java.util.List;

import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.classfile.ClassDescriptor;

public abstract class XMLParameterIteratingDetector implements RecipeDetector {

	private List<PredefInstance> predefInstances = new ArrayList<PredefInstance>();
	protected BugReporter bugReporter;

	public XMLParameterIteratingDetector(BugReporter bugReporter) {
		this.bugReporter = bugReporter;
		predefInstances = FindBugsUtil.loadPredefInstances(getClass(), getPredefName(), bugReporter);
	}


	protected BugInstance createBugInstance(PredefInstance predefInstance, String primaryClass, int priority) {
		return FindBugsUtil.createBugInstance(this, getBugPattern(), predefInstance, primaryClass, priority);
	}
	
	protected abstract String getBugPattern();

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.umd.cs.findbugs.Detector2#getDetectorClassName()
	 */
	public String getDetectorClassName() {
		return getClass().getName();
	}

	protected abstract String getPredefName();

	public void report() {
		for (PredefInstance instance : predefInstances) {
			report(instance);
		}
	}

	public void visitClassContext(ClassDescriptor classDescriptor) {
		for (PredefInstance instance : predefInstances) {
			visitClassContext(classDescriptor, instance);
		}
	}

}
